/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: CamundaApplication
 * @Package com.rx.camunda
 * @Description: 统一流程中心
 * @author: 陈锦韬
 * @date: 2021\6\9 0009
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 *
 */
package com.rx.camunda;

import com.rx.core.anno.RxSpringBootApplication;
import com.rx.core.base.RxBaseSpringApplication;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.*;
import org.camunda.bpm.engine.identity.User;
import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.spring.SpringProcessEngineConfiguration;
import org.camunda.bpm.spring.boot.starter.event.PostDeployEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.context.event.EventListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Administrator
 * @Description: 统一流程中心
 * @date: 2021\6\9 0009
 */
@RxSpringBootApplication(
        componentPackages={"com.rx.core.*","com.rx.camunda.*"}
        ,dataPackages={"com.rx.camunda.vo"} // 不能带星号
        ,handlerPackages={"com.rx.camunda.handler"} // 不能带星号
        )
@EnableCircuitBreaker
@Slf4j
public class CamundaApplication implements CommandLineRunner {
    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Value("${camunda.bpm.process-engine-name:default}")
    String engineName;

    public static void main(String[] args) {
        RxBaseSpringApplication.run(CamundaApplication.class, args);


    }

    @Override
    public void run(String... args) throws Exception {
        List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery().active().latestVersion().list();
        log.info("> 处于激活状态的最新版本的流程定义数量: " + processDefinitionList.size());
        for (ProcessDefinition pd : processDefinitionList) {
            log.info("\t ===> Process definition: " + pd.getKey() + " 版本：" + pd.getVersion());
        }
        // 获取引擎 ProcessEngineImpl 实现服务类
        ProcessEngine processEngine = ProcessEngines.getProcessEngine(engineName);
        // 流程引擎信息类
        //ProcessEngineInfo processEngineInfo = ProcessEngines.getProcessEngineInfo(engineName);
        //log.info("ResourceUrl" + processEngineInfo.getResourceUrl());
        //protected static Map<String, ProcessEngine> processEngines
        //protected static Map<String, ProcessEngineInfo> processEngineInfosByName
        //protected static Map<String, ProcessEngineInfo> processEngineInfosByResourceUrl
        //protected static List<ProcessEngineInfo> processEngineInfos

        // 配置类
        ProcessEngineConfiguration processEngineConfiguration = processEngine.getProcessEngineConfiguration();

        // beans
        // Map<Object, Object> beans = ((ProcessEngineConfigurationImpl) processEngineConfiguration).getBeans();
        // 部署 流程定义
        RepositoryService repository = processEngine.getRepositoryService();
        // 授权
        AuthorizationService authorizationService = processEngine.getAuthorizationService();
        // CMMN
        CaseService caseService = processEngine.getCaseService();
        // DMN 决策
        DecisionService decisionService = processEngine.getDecisionService();
        // 外部任务
        ExternalTaskService externalTaskService = processEngine.getExternalTaskService();
        // 过滤
        FilterService filterService = processEngine.getFilterService();
        // 表单
        FormService formService = processEngine.getFormService();
        // 历史
        HistoryService historyService = processEngine.getHistoryService();
        // 人员 组织
        IdentityService identityService = processEngine.getIdentityService();
        for (User user : identityService.createUserQuery().list()) {
            log.info(user.getEmail());
            log.info(user.getId());
            log.info(user.getFirstName());
            log.info(user.getLastName());
        }
        // 管理 cmd job相关
        ManagementService managementService = processEngine.getManagementService();
        // 当前实例
        RuntimeService runtimeService = processEngine.getRuntimeService();
        // 当前用户任务
        TaskService taskService = processEngine.getTaskService();




        if (repository.equals(repositoryService)){
            log.info("相同");
        }

    }

    @EventListener
    private void processPostDeploy(PostDeployEvent e){
        // 启动
        runtimeService.startProcessInstanceByKey("process");
    }

}
